//
//  RepairInformationView.swift
//  PropertyCommunity
//
//  Created by benwang on 2021/6/27.
//

import UIKit

extension PTSheetHandle {
  func sheet(repairInfo: RepairInformationModel?, handle: @escaping Handle) {
    let sheet = R.nib.repairInformationView(owner: nil)!
    sheet.repairInfoModel = repairInfo
    self.sheet(PTSheetHandle.Content(sheet: sheet), handle: handle)
  }
}

class RepairInformationView: UIView {

  @IBOutlet weak var addressOutlet: PlaceholderTextView!

  @IBOutlet weak var holderOutlet: UITextField!

  @IBOutlet weak var phoneOutlet: UITextField!

  @IBOutlet weak var nextStepOutlet: UIButton!

  private var completeHandle: PTSheetHandle.Handle?

  var repairInfoModel: RepairInformationModel? {
    didSet {
      addressOutlet.text = repairInfoModel?.address
      holderOutlet.text = repairInfoModel?.holder
      phoneOutlet.text = repairInfoModel?.phone
    }
  }

  override func awakeFromNib() {
    super.awakeFromNib()
    addressOutlet.placeholder = "请输入小区-楼栋-单元-房间号"

    holderOutlet.leftViewMode = .always
    holderOutlet.leftView = UIView(frame: .init(x: 0, y: 0, width: 12, height: 0))

    phoneOutlet.leftViewMode = .always
    phoneOutlet.leftView = UIView(frame: .init(x: 0, y: 0, width: 12, height: 0))
  }

  @IBAction func tappedCancel(_ sender: Any) {
    completeHandle?(.cancel)
  }

  @IBAction func tappedNextStep(_ sender: Any) {
    repairInfoModel?.address = addressOutlet.text
    repairInfoModel?.holder = holderOutlet.text ?? ""
    repairInfoModel?.phone = phoneOutlet.text ?? ""
    completeHandle?(.ensure)
  }
}

extension RepairInformationView: PTSheetConfigurable {

  var handle: PTSheetHandle.Handle? {
      get {
          return completeHandle
      }
      set {
          completeHandle = newValue
      }
  }

  func shouldUpdateLayoutConstraints() {

    self.snp.remakeConstraints { (make) in
      make.height.equalTo(449)
      make.center.equalToSuperview()
      make.width.lessThanOrEqualTo(320)
      make.left.equalToSuperview().offset(47).priority(.high)
      make.right.equalToSuperview().offset(-47).priority(.high)
    }
  }
}
